package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication.api.ApiService;
import com.example.myapplication.model.User;
import com.example.myapplication.network.RetrofitClient;
import com.example.myapplication.util.DBConnect;

import java.sql.Connection;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    // 数据库连接
    DBConnect dbConnect = new DBConnect();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        Connection connection =dbConnect.getConnection();
        System.out.println(connection);
        // 获取 Retrofit 服务实例
        ApiService apiService = RetrofitClient.getApiService();
        System.out.println(111111);
        // 示例：获取所有用户
        Call<List<User>> call = apiService.getUsers();
        call.enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {
                if (response.isSuccessful()) {
                    List<User> users = response.body();
                    System.out.println("Users: " + users);
                    Log.d("MainActivity", "Users: " + users);
                } else {
                    System.out.println("Users: " + response.code() + " - " + response.message());
                    Log.e("MainActivity", "Error Code: " + response.code());
                }
            }

            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {
                Log.e("MainActivity", "Error: " + t.getMessage());
            }
        });

        // 示例：创建新用户
        User newUser = new User("Alice", "123456");
        Call<User> createCall = apiService.createUsers(newUser);
        createCall.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful()) {
                    User createdUser = response.body();
                    Log.d("MainActivity", "Created User: " + createdUser);
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Log.e("MainActivity", "Error: " + t.getMessage());
            }
        });



    }
}